<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">

      <div class="title-container">
        <img src="http://likede2-admin.itheima.net/img/logo.595745bd.png" alt="" class="title-img">
      </div>

      <el-form-item prop="loginName">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <!-- 账号/用户名 -->
        <el-input
          ref="loginName"
          v-model="loginForm.loginName"
          placeholder="请输入账号"
          name="loginName"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>
      <!-- 密码 -->
      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="请输入密码"
          name="password"
          tabindex="2"
          auto-complete="on"
        />
        <!-- 密码右边的小眼睛  判断是张开还是闭上-->
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>

      <!-- 验证码 -->
      <el-row type="flex">
        <el-col>
          <el-form-item prop="code">
            <span class="svg-container">
              <svg-icon icon-class="user" />
            </span>
            <el-input
              v-model="loginForm.code"
              placeholder="请输入验证码"
              name="code"
              type="text"
              tabindex="3"
              auto-complete="on"
            />
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <div class="imgCode" @click="getAuthCode()"> <img :src="codeSrc" alt=""></div>
        </el-col>
      </el-row>
      <!-- 登录按钮 -->
      <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.prevent="handleLogin">登录</el-button>

    </el-form>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'
import { getAuthCodeAPI } from '@/api/user'
export default {
  name: 'Login',
  data() {
    // 账号的自定义
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('请输入账号'))
      } else {
        callback()
      }
    }
    // 密码的自定义
    const validatePassword = (rule, value, callback) => {
      if (value.length < 5) {
        callback(new Error('密码不能小于5位'))
      } else {
        callback()
      }
    }
    // 验证码的自定义
    const validateVCode = (rule, value, callback) => {
      // console.log(value)
      if (value.length < 4) {
        callback(new Error('请输入四位验证码'))
      } else {
        callback()
      }
    }
    return {
      codeSrc: '', // 验证码图片
      loginForm: {
        loginName: 'admin',
        password: 'admin',
        clientToken: 'zAJY7FPflzQpRH2sbKa8KblQgWIP1RHC',
        code: '', // 输入的验证码
        loginType: 0 // 后台
      },
      // 自定义表单验证
      loginRules: {
        loginName: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }],
        code: [{ required: true, trigger: 'blur', validator: validateVCode }]
      },
      //
      loading: false, // 是否加载中状态
      passwordType: 'password', // 密码
      redirect: undefined
    }
  },
  watch: {},
  created() {
    this.getAuthCode() // 调用
  },
  methods: {
    // 控制密码右边的小眼睛
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },

    // 获取验证码图片
    async getAuthCode() {
      const { data } = await getAuthCodeAPI(this.loginForm.clientToken)
      const windowUrl = window.URL || window.webkitURL // 处理浏览器兼容性
      this.codeSrc = windowUrl.createObjectURL(data)
    },

    handleLogin() {
      this.$refs.loginForm.validate(async valid => {
        if (valid) {
          try {
            this.loading = true
            await this.$store.dispatch('user/loginAction', this.loginForm)
          } finally {
            this.loading = false
          }
          this.$router.push('/') // 跳转
        }
      })
    }

  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: #999;
      height: 47px;
      caret-color: #999;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid #ccc;
    // background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}

</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-image: url(~@/assets/bglogin.png);
  overflow: hidden;

  .login-form {
    position: relative;
    width: 478px;
    height: 388px;
    max-width: 100%;
    padding: 0px 35px 0;
    border-radius: 10px;
    margin:0 auto;
    margin-top: 200px;
    // overflow: hidden;
    background-color: #fff;
    box-sizing: border-box;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;
.title-img{
width: 96px;
height: 96px;
position: relative;
top: -45px;
    left: 162px;
}

  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}

</style>
